package com.atguigu.dga.governance.assess.impl.spec;


import com.atguigu.dga.governance.assess.Assessor;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import com.atguigu.dga.governance.bean.GovernanceMetric;
import com.atguigu.dga.meta.bean.TableMetaInfo;
import com.atguigu.dga.meta.constants.MetaConst;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//参考建数仓表规范
//ODS层 ：开头:ods  结尾 :inc/full
//结构ods_xx_( inc|full)     ^ods_\w+_(inc|full)$
//DIM层 :  dim开头     full/zip 结尾
//结构: ods_xx_( inc|full)
//DWD层:  dwd 开头  inc/full 结尾
//结构： dwd_xx_xx_(inc|full)
//DWS层： dws开头
//结构dws_xx_xx_xx_ (1d/nd/td)
//ADS层： ads 开头
//结构 ads_xxx
//符合则 10分，否则0分
//OTHER：
//未纳入分层，给5分

@Component("TABLE_NAME_STANDARD")
public class TableNameStandardAssessor extends Assessor {

    Pattern odsPattern = Pattern.compile("^ods_.+_(inc|full)$");
    Pattern dwdPattern =Pattern.compile("^dwd_.+_.+_(inc|full)$");
    Pattern dimPattern =Pattern.compile("^dim_.+_(zip|full)$");
    Pattern dwsPattern =Pattern.compile("^dws_.+_.+_.+_(\\d+d|td|nd)$");
    Pattern adsPattern =Pattern.compile("^ads_.+");
    Pattern dmPattern =Pattern.compile("^dm_.+");


    @Override
    protected void assess(AssessParam assessParam, GovernanceAssessDetail governanceAssessDetail) throws ParseException {

        TableMetaInfo tableMetaInfo = assessParam.getTableMetaInfo();
        GovernanceMetric governanceMetric = assessParam.getGovernanceMetric();

        //1.获取到当前表的表名
        String tableName = tableMetaInfo.getTableName();
        //2.获取层级
        String dwLevel = tableMetaInfo.getTableMetaInfoExtra().getDwLevel();

        if (dwLevel.equals(MetaConst.DW_LEVEL_OTHER)){
            governanceAssessDetail.setAssessScore(BigDecimal.valueOf(5));
            governanceAssessDetail.setAssessProblem("未纳入分层");
            String url = governanceMetric.getGovernanceUrl() + tableMetaInfo.getId();
            governanceAssessDetail.setGovernanceUrl(url);
            return;
        }

        //匹配表名 每一层都有自己的正则表达式匹配
        //思路：先将每一次的正则表达式定义好  然后根据这个表所在的层级获取对应的 正则表达式 然后进行匹配
        Pattern pattern = null;
        switch (tableMetaInfo.getTableMetaInfoExtra().getDwLevel()){
            case "ODS" : pattern= odsPattern  ; break;
            case "DWD" : pattern= dwdPattern  ; break;
            case "DWS" : pattern= dwsPattern  ; break;
            case "DIM" : pattern= dimPattern  ; break;
            case "DM" :  pattern= dmPattern  ; break;
            case "ADS" : pattern= adsPattern  ; break;
        }


        //拿着表名和正则表达式匹配
        Matcher matcher = pattern.matcher(tableName);
        if (!matcher.matches()){//匹配不上则给0分
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem("表名不合规泛");

        }

    }
}
